;
(function (window, angular) {
    'use strict';
    angular.module("Controller.BasicData.MaterialInfoRecord", [])
        .controller("MaterialInfoRecordCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            $scope.markInfo = {
                curRow: -1
            };

            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
            };
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.syncInfo = {};
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.syncData = function() {
                if (!$scope.syncInfo.timeStart || !$scope.syncInfo.timeEnd) {
                    ybWidget.ShowMessage("请选择时间范围", MESG_TYPE.ERROR);
                    return
                }

                const tStart = $filter("YbDate")($scope.syncInfo.timeStart, 'yyyy-MM-dd')
                const tEnd = $filter("YbDate")($scope.syncInfo.timeEnd, 'yyyy-MM-dd')
                var reqUrl = `/materialInfoRecord/manulSync?timeStart=${tStart}&timeEnd=${tEnd}`;
                ybHttp.Post("device", reqUrl).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };

            $scope.condition = {};
            $scope.dataList = [];

            $scope.tbConf = {
                scrollWidth: 1500,
                tableCol: [
                    {
                        name: "采购信息记录号",
                        dataField: "purchaseInfoRecordNo",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件编号",
                        dataField: "materialNo",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialDesc",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "净价",
                        dataField: "netPrice",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "装运指令",
                        dataField: "shipInstruct",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "供货商或债权人帐号",
                        dataField: "supplierAccount",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "供货商或债权人名字",
                        dataField: "name",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "采购组织",
                        dataField: "purchaseOrg",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "工厂",
                        dataField: "factory",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "记录有效起始日",
                        dataField: "conditionRecordValidDatesShow",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "记录有效截止日期",
                        dataField: "conditionRecordValidDateeShow",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "价格单位",
                        dataField: "priceUnit",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "货币码",
                        dataField: "cuky",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "销售/购买税代码",
                        dataField: "taxCode",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "创建日期",
                        dataField: "recordCreateDateShow",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "创建人",
                        dataField: "recordCreateName",
                        width: 140,
                        isShow: true
                    },
                ]
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index;
            };

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }

                var service = "device";
                var reqUrl = "/materialInfoRecord/list";
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.conditionRecordValidDatesShow = $filter("YbDate")(item.conditionRecordValidDates, 'yyyy-MM-dd HH:mm:ss');
                        item.conditionRecordValidDateeShow = $filter("YbDate")(item.conditionRecordValidDatee, 'yyyy-MM-dd HH:mm:ss');
                        item.recordCreateDateShow = $filter("YbDate")(item.recordCreateDate, 'yyyy-MM-dd HH:mm:ss');
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            (function () {
                getDataList();
            })();
        }])
    ;
})(window, window.angular);